<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>WebSocket</title>
</head>
<body>

    <div id="msg"></div>
</body>
<script src="/jquery/jquery-3.3.1.js"></script>
<script data-th-inline="javascript">
/*<![CDATA[*/

    var ws = new WebSocket("ws://localhost:9090/demo/ws");

    function sendMsg() {
        ws.send("page:发送消息");
    }

    // 接收消息
    ws.onmessage = function(evt) {
        $("#msg").append(evt.data + '<br>');
    };

    ws.onopen = function() {
        $("#msg").append("websocket 连接成功<br>");
        sendMsg();
    };

// ws.addEventListener('open', function (event) {
//     sendMsg();
// });

    ws.onclose = function() {
        $("#msg").append("websocket 已关闭");
    };

    ws.onerror = function(event) {
        debugger;
        $("#msg").append(event);
    }

/*]]>*/
</script>
</html>